﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKExcelTableUtil;
using YJKRevitView;
using YJKExcelFormTool;
using YJKExcelFormTool.Designer;

namespace YJKRoomPractice
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdRoomPractice : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			UIDocument activeUIDocument = cmdData.Application.ActiveUIDocument;
			Document document = activeUIDocument.Document;
			Transaction transaction = new Transaction(document, "modify Scale");
			int scale = activeUIDocument.ActiveView.Scale;
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			try
			{
				RoomPracticeForm roomPracticeForm = new RoomPracticeForm(cmdData, document);
				if (!roomPracticeForm.m_bIsExit)
				{
					YJKMessageBox.Show("请布置房间");
					return Autodesk.Revit.UI.Result.Succeeded;
				}
				roomPracticeForm.ShowDialog();
				if (roomPracticeForm.DialogResult == DialogResult.OK)
				{
					if (RoomPracticeForm.m_dicFloorToRowNumber.Count == 0)
					{
						YJKMessageBox.Show("当前项目无房间信息");
						return Autodesk.Revit.UI.Result.Succeeded;
					}
					TableSplit tableSplit = new TableSplit(cmdData, roomPracticeForm.grid);
					XYZ zero = XYZ.Zero;
					if (TableInterface.GridDrawView(cmdData, "表格工具") == ViewSelectResult.VSR_Cancel)
					{
						return Autodesk.Revit.UI.Result.Failed;
					}
					transaction.Start();
					activeUIDocument.ActiveView.Scale = scale;
					transaction.Commit();
					GridDataRefresh gridDataRefresh = new GridDataRefresh();
					gridDataRefresh.GridSetInfo.HeadRows = 2;
					bool flag = false;
					bool flag2 = false;
					while (tableSplit.GetSplitAfterTable(ref flag, ref flag2) == null)
					{
						if (flag)
						{
							YJKMessageBox.Show("表格生成完成");
							break;
						}
						GridToolExportImportRevit gridToolExportImportRevit = new GridToolExportImportRevit(cmdData, tableSplit.m_pt);
						if (gridToolExportImportRevit.SetGrid(cmdData, tableSplit.m_newGrid, CREATENEWVIEW.NONE))
						{
							gridToolExportImportRevit.SetRefreshData(gridDataRefresh);
							gridToolExportImportRevit.DrawInRevitView();
						}
						if (flag2)
						{
							YJKMessageBox.Show("表格生成完成");
							break;
						}
					}
				}
			}
			catch (Exception)
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
